<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      //   用于实现平移动画实现的数据
      startValue: 0,
      endValue: 9,
      //   增加 timerId 的变量, 并且增加一个方法 startInterval ,来控制 startValue 和 endValue 的值
      timeId: null
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('click', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('click', this.screenAdapter)
    clearInterval(this.timeId)
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, 'chalk')
      const initOption = {
        title: {
          text: '▎ 地区销售排行',
          left: 20,
          top: 20
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar'
          }
        ],
        grid: {
          top: '40%',
          left: '5%',
          bottom: '5%',
          right: '5%',
          containLabel: true
        },
        tooltip: {
          show: true
        }
      }
      this.chartInstance.setOption(initOption)
      //   设置鼠标移入时停止动画
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timeId)
      })
      //   鼠标移出时，开始动画
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData() {
      //   获取服务器数据
      const { data: res } = await this.$http.get('rank')
      this.allData = res
      //   console.log(this.allData)
      // 对数据进行排序，从大到小排序
      this.allData.sort((a, b) => {
        return b.value - a.value
      })
      this.updateChart()
      this.startInterval()
    },
    updateChart() {
      // 不同柱显示不同的颜色
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5']
      ]
      // 处理图标所需的数据
      const provinceArr = this.allData.map(item => {
        return item.name
      })
      const valueArr = this.allData.map(item => {
        return item.value
      })
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        // 将startValue和endValue应用在dataZoom上，并隐藏dataZoom的显示
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: arg => {
                // console.log(arg)
                let targetColor = colorArr[0]
                if (arg.value >= 300) {
                  targetColor = colorArr[0]
                } else if (arg.value >= 200) {
                  targetColor = colorArr[1]
                } else {
                  targetColor = colorArr[2]
                }
                return new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: targetColor[0]
                  },
                  {
                    offset: 1,
                    color: targetColor[1]
                  }
                ])
              }
            }
          }
        ]
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter() {
      // 当窗口大小改变时，重新设置样式
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    startInterval() {
      if (this.timeId) {
        clearInterval(this.timeId)
      }
      this.timeId = setInterval(() => {
        this.startValue++
        this.endValue++
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 9
        }
        this.updateChart()
      }, 3000)
    }
  }
}
</script>
<style lang="less" scoped></style>
